//
//  PopoverAnimator.swift
//  mySwift
//
//  Created by 高赛 on 16/9/20.
//  Copyright © 2016年 高赛. All rights reserved.
//

import UIKit

protocol PopoverAnimatorDelegate: NSObjectProtocol {
    
    func change()
    
}

class PopoverAnimator: NSObject,UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    
    // 标志当前是展现还是消失
    var isPresnt: Bool = false
    // 定义属性保存菜单的大小
    var presentFrame = CGRect.zero
    // 设置代理
    weak var delegate : PopoverAnimatorDelegate?
    
    
    // 实现代理方法, 告诉系统谁来负责过渡动画
    // UIPresentationController iOS8推出的专门用于过渡动画的
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let pc = PopoverViewPresentationController(presentedViewController: presented, presenting: presenting)
        // 设置菜单的大小
        pc.presentFrame = presentFrame
        
        return pc
    }
    
    // 告诉系统谁来负责modal的消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresnt = false
        
        return self
        
    }
    
    // 告诉系统谁来负责modal的展现动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresnt = true
        
        return self
    }
    
    // 动画的时长
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        if isPresnt {
            return 0.5
        }else {
            return 0.2
        }
        
    }
    // 告诉系统如何动画,无论展现还是消失都会调用这个方法
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        // 拿到要展现的视图
        //        let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
        // 拿到展现前的视图
        //        let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
        
        if isPresnt {
            // 拿到要展现的视图的view
            let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)
            
            toView?.transform = CGAffineTransform(scaleX: 1.0, y: 0)
            // 注意 一定要讲视图添加到容器上
            transitionContext.containerView.addSubview(toView!)
            // 设置锚点 不然会在中间展开
            toView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            // 设置动画
            // 将Duration时间参数用transitionDuration(using: transitionContext)来返回
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                toView?.transform = CGAffineTransform.identity
            }) { (_) in
                transitionContext.completeTransition(true)
            }
        }else {
            let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)
            // 将Duration时间参数用transitionDuration(using: transitionContext)来返回
            UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
                // 由于CGFloat是不准确的, 所以写0.0会没有动画 所以要写0.0001
                fromView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.0001)
                }, completion: { (_) in
                    transitionContext.completeTransition(true)
            })
        }
    } 
}
